using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.DAL.BDHomePage;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_SiteSettingsEdit : SiteAdminBasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        SiteSettings siteSettings;

        if (Int32.TryParse(Request["id"], out id))
            siteSettings = SiteSettings.Load(id);
        else
            siteSettings = null;

        if (siteSettings != null)
        {
            if (!string.IsNullOrEmpty(siteSettings.AdminMailAddress))
				tbxAdminMailAddress.Text = siteSettings.AdminMailAddress;
			if (!string.IsNullOrEmpty(siteSettings.SupportMailAddress))
				tbxSupportMailAddress.Text = siteSettings.SupportMailAddress;
			if (!string.IsNullOrEmpty(siteSettings.SmtpServerAddress))
				tbxSmtpServerAddress.Text = siteSettings.SmtpServerAddress;
			if (!string.IsNullOrEmpty(siteSettings.SmtpServerUsername))
				tbxSmtpServerUsername.Text = siteSettings.SmtpServerUsername;
			if (!string.IsNullOrEmpty(siteSettings.SmtpServerPassword))
				tbxSmtpServerPassword.Text = siteSettings.SmtpServerPassword;
			tbxSmtpServerPort.Text = siteSettings.SmtpServerPort.ToString();
			if (!string.IsNullOrEmpty(siteSettings.DefaultThemeName))
				tbxDefaultThemeName.Text = siteSettings.DefaultThemeName;
			tbxDefaultUserRoleId.Text = siteSettings.DefaultUserRoleId.ToString();
			if (!string.IsNullOrEmpty(siteSettings.SuperAdminUsername))
				tbxSuperAdminUsername.Text = siteSettings.SuperAdminUsername;
			if (!string.IsNullOrEmpty(siteSettings.SuperAdminPassword))
				tbxSuperAdminPassword.Text = siteSettings.SuperAdminPassword;
			if (!string.IsNullOrEmpty(siteSettings.SiteName))
				tbxSiteName.Text = siteSettings.SiteName;
			if (!string.IsNullOrEmpty(siteSettings.SiteBaseUrl))
				tbxSiteBaseUrl.Text = siteSettings.SiteBaseUrl;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string adminMailAddress = Security.BindStringInput(tbxAdminMailAddress.Text, _securityLevel);
		string supportMailAddress = Security.BindStringInput(tbxSupportMailAddress.Text, _securityLevel);
		string smtpServerAddress = Security.BindStringInput(tbxSmtpServerAddress.Text, _securityLevel);
		string smtpServerUsername = Security.BindStringInput(tbxSmtpServerUsername.Text, _securityLevel);
		string smtpServerPassword = Security.BindStringInput(tbxSmtpServerPassword.Text, _securityLevel);
		int smtpServerPort = Security.BindIntegerInput(tbxSmtpServerPort.Text, _securityLevel);
		string defaultThemeName = Security.BindStringInput(tbxDefaultThemeName.Text, _securityLevel);
		int defaultUserRoleId = Security.BindIntegerInput(tbxDefaultUserRoleId.Text, _securityLevel);
		string superAdminUsername = Security.BindStringInput(tbxSuperAdminUsername.Text, _securityLevel);
		string superAdminPassword = Security.BindStringInput(tbxSuperAdminPassword.Text, _securityLevel);
		string siteName = Security.BindStringInput(tbxSiteName.Text, _securityLevel);
		string siteBaseUrl = Security.BindStringInput(tbxSiteBaseUrl.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (SiteSettings.Save(
				id, 
				adminMailAddress,
				supportMailAddress,
				smtpServerAddress,
				smtpServerUsername,
				smtpServerPassword,
				smtpServerPort,
				defaultThemeName,
				defaultUserRoleId,
				superAdminUsername,
				superAdminPassword,
				siteName,
				siteBaseUrl
                ))
                    Response.Redirect("sitesettingslist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            SiteSettings siteSettings = SiteSettings.Create(
				adminMailAddress,
				supportMailAddress,
				smtpServerAddress,
				smtpServerUsername,
				smtpServerPassword,
				smtpServerPort,
				defaultThemeName,
				defaultUserRoleId,
				superAdminUsername,
				superAdminPassword,
				siteName,
				siteBaseUrl
                );

            if (siteSettings != null)
                Response.Redirect("sitesettingslist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("sitesettingslist.aspx");
    }
}
